﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using ComBoxTest.Properties;

namespace ComBoxTest
{
    public partial class MyComBox : ComboBox
    {
        protected Image normalImage = Image.FromStream(Assembly.GetExecutingAssembly().GetManifestResourceStream(@"ComBoxTest.Res.normal.png"));
        
        Image imgTopLeft = (Image)Resources.topleft;//窗体顶部左上角图片
        Image imgTopRight = (Image)Resources.topright;//窗体顶部右上角图片
        Image imgTopMiddle = (Image)Resources.topstretch;//窗体顶部中间图片

        Image imgBottomLeft = (Image)Resources.bottomLeft;//窗体底部左下角图片
        Image imgBottonRight = (Image)Resources.bottomRight;//窗体底部右下角图片
        Image imgBottonmMiddle = (Image)Resources.bottomstretch;//窗体底部中间图片

        Image imgMiddleLeft = (Image)Resources.LeftDrag_Mid;//窗体中部左边框图片
        Image imgMiddleMid = (Image)Resources.MidDrag_Mid;
        Image imgMiddleRight = (Image)Resources.RightDrag_Mid;//窗体中部右边框图片

        [System.Runtime.InteropServices.DllImport("user32.dll ")]
        static extern IntPtr GetWindowDC(IntPtr hWnd);
        [System.Runtime.InteropServices.DllImport("user32.dll ")]
        static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC); 

        public MyComBox()
        {
            InitializeComponent();
            //SetStyle(ControlStyles.UserPaint, true);
        }

        protected override void WndProc(ref   Message m)
        {
            base.WndProc(ref   m);
            if (m.Msg == 0xf || m.Msg == 0x133)           //拦截系统消息 
            {
                IntPtr hDC = GetWindowDC(m.HWnd);
                if (hDC.ToInt32() == 0)
                { return; }

                Graphics g = Graphics.FromHdc(hDC);

                //Graphics gra = Graphics.FromImage(normalImage);
                //ControlPaint.DrawComboButton(gra, new Rectangle(0, 0, Width, Height), ButtonState.Normal);

                Graphics gfx = Graphics.FromHwnd(this.Handle);
                //顶部左边
                gfx.DrawImage(imgTopLeft, 2, 2, imgTopLeft.Width, imgTopLeft.Height);
                //顶部右边
                gfx.DrawImage(imgTopRight, Width - imgTopRight.Width, 0, imgTopRight.Width, imgTopRight.Height);
                //顶部中间
                gfx.DrawImage(imgTopMiddle, imgTopLeft.Width, 0, Width - imgTopLeft.Width - imgTopRight.Width, imgTopMiddle.Height);

                //底部左边
                gfx.DrawImage(imgBottomLeft, 0, Height - imgBottomLeft.Height,imgBottomLeft.Width,imgBottomLeft.Height);
                //底部右边
                gfx.DrawImage(imgBottonRight,Width - imgBottonRight.Width,Height - imgBottonRight.Height,imgBottonRight.Width,imgBottonRight.Height);
                //底部中间
                gfx.DrawImage(imgMiddleMid,imgMiddleLeft.Width,Height - imgMiddleMid.Height,Width - imgMiddleLeft.Width - imgMiddleRight.Width,imgMiddleMid.Height);

                gfx.DrawImage(normalImage, Width - 18, 1);
                gfx.Flush();
                gfx.Dispose();
                Console.WriteLine("DrawComboButton " + g);

                //画边框的   
                //ControlPaint.DrawBorder(g, new Rectangle(0, 0, Width, Height), Color.Red, ButtonBorderStyle.Solid);
                //画坚线  
                //ControlPaint.DrawBorder(g, new Rectangle(Width - Height, 0, Height, Height), Color.Red, ButtonBorderStyle.Solid);
                //g.DrawLine(new Pen(Brushes.Blue, 2), new PointF(this.Width - this.Height, 0), new PointF(this.Width - this.Height, this.Height));  

                m.Result = IntPtr.Zero;           //返回结果 
                ReleaseDC(m.HWnd, hDC);           //释放 
            }
        } 

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            e.Graphics.DrawImage(normalImage, this.Location);
            Console.WriteLine("ONpaint");
            //Invalidate();
            //e.Graphics.Clear(System.Drawing.Color.Red);
        }

        protected override void OnPaintBackground(PaintEventArgs pevent)
        {
            base.OnPaintBackground(pevent);
            pevent.Graphics.DrawImage(normalImage, this.Location);
            Console.WriteLine("OnPaintBackground");
            pevent.Graphics.Clear(System.Drawing.Color.Red);
        }
    }
}
